package com.web.gobang.view;

import com.web.gobang.data.Player;

import javax.naming.Name;
import javax.swing.*;
import java.awt.*;

/**
 * 界面显示 之 玩家信息面板，显示玩家昵称，地址等信息
 */
public class UserPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    static JPanel myPanel, otherPanel;
    private static JLabel myName, myAddress, myColor, myGrade;
    private static JLabel otherName, otherAddress, otherColor, otherGrade;
    static UserPanel userPanel;
    /**
     * 用户信息面板，左面版(我的信息)
     */
    public static final int left = 0;
    /**
     * 用户信息面板，右面版(对方信息)
     */
    public static final int right = 1;

//    public void gridLable(JLabel label,GridBagConstraints constraints,GridBagLayout gridBagLayout) {
//        gridBagLayout.setConstraints(label,constraints);
//        myPanel.add(label);
//    }

    public UserPanel() {
        this.setVisible(true);
        this.setLayout(null);
        this.setBackground(new Color(180, 180, 180));

        myPanel = new JPanel();
        otherPanel = new JPanel();

//        GridBagLayout gbaglayout=new GridBagLayout();    //创建GridBagLayout布局管理器
//        GridBagConstraints constraints=new GridBagConstraints();

        myPanel.setLayout(null);
        otherPanel.setLayout(null);

        ImageIcon playerIcon = new ImageIcon("./img/player1.png");
        playerIcon.setImage(playerIcon.getImage().getScaledInstance(70, 70, Image.SCALE_SMOOTH));
        JLabel player = new JLabel(playerIcon);

        ImageIcon playerIcon2 = new ImageIcon("./img/player2.png");
        playerIcon2.setImage(playerIcon2.getImage().getScaledInstance(70, 70, Image.SCALE_SMOOTH));
        JLabel player2 = new JLabel(playerIcon2);

        ImageIcon blackChessIcon = new ImageIcon("./img/blackChess.png");
        blackChessIcon.setImage(blackChessIcon.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        JLabel blackChess = new JLabel(blackChessIcon);

        ImageIcon whiteChessIcon = new ImageIcon("./img/whiteChess.png");
        whiteChessIcon.setImage(whiteChessIcon.getImage().getScaledInstance(30, 30, Image.SCALE_SMOOTH));
        JLabel whiteChess = new JLabel(whiteChessIcon);

        ImageIcon pointIcon = new ImageIcon("./img/point.png");
        pointIcon.setImage(pointIcon.getImage().getScaledInstance(70, 30, Image.SCALE_SMOOTH));
        JLabel point = new JLabel(pointIcon);
        JLabel point2 = new JLabel(pointIcon);

        myName = new JLabel("玩家昵称: ");
        myAddress = new JLabel("玩家地址: ");
        myGrade = new JLabel("玩家分数");

        otherName = new JLabel("玩家昵称: ");
        otherAddress = new JLabel("玩家地址: ");
        otherGrade = new JLabel("玩家分数");

//        constraints.fill = GridBagConstraints.BOTH;    //组件填充显示区域
//        constraints.weightx=0.4;    // 指定组件的分配区域
//        constraints.weighty=0.2;
//        constraints.gridwidth=1;

//        gridLable(player,constraints,gbaglayout);
//        gridLable(blackChess,constraints,gbaglayout);
//        constraints.gridwidth = GridBagConstraints.REMAINDER;
//        gridLable(myName,constraints,gbaglayout);
//
//        constraints.gridwidth=1;
//        gridLable(point,constraints,gbaglayout);
//        constraints.gridwidth = GridBagConstraints.REMAINDER;
//        gridLable(myGrade,constraints,gbaglayout);
//
//        constraints.gridwidth=1;
//        gridLable(myAddress,constraints,gbaglayout);
        player.setBounds(10,40,playerIcon.getIconWidth(),playerIcon.getIconHeight());
        myPanel.add(player);

        myName.setBounds(10,100,100,50);
        myPanel.add(myName);

        blackChess.setBounds(120,50,blackChessIcon.getIconWidth(),blackChessIcon.getIconHeight());
        myPanel.add(blackChess);

        myAddress.setBounds(10,5,300,30);
        myPanel.add(myAddress);

        point.setBounds(150,80,pointIcon.getIconWidth(),pointIcon.getIconHeight());
        myPanel.add(point);

        myGrade.setBounds(220,75,100,50);
        myPanel.add(myGrade);

        player2.setBounds(10,40,playerIcon.getIconWidth(),playerIcon.getIconHeight());
        otherPanel.add(player2);

        otherName.setBounds(10,100,100,50);
        otherPanel.add(otherName);

        whiteChess.setBounds(120,50,blackChessIcon.getIconWidth(),blackChessIcon.getIconHeight());
        otherPanel.add(whiteChess);

        otherAddress.setBounds(10,5,300,30);
        otherPanel.add(otherAddress);

        point2.setBounds(150,80,pointIcon.getIconWidth(),pointIcon.getIconHeight());
        otherPanel.add(point2);

        otherGrade.setBounds(220,75,100,50);
        otherPanel.add(otherGrade);

        this.add(myPanel);
        this.add(otherPanel);
        userPanel = this;
    }

    /**
     * 界面显示，控件加载完毕后执行
     */
    public static void init() {
        myPanel.setBounds(2, 2, userPanel.getWidth() / 2 - 2, userPanel.getHeight() - 5);
        otherPanel.setBounds(userPanel.getWidth() / 2 + 2, 2, userPanel.getWidth() / 2 - 2,
                userPanel.getHeight() - 5);

        setUserInfo(null, left);
        setUserInfo(null, right);
        userPanel.repaint();
    }

    /**
     * 在用户信息版面，显示用户信息
     */
    public static void setUserInfo(Player player, int position) {
        if (player == null) {
            player = new Player();
        }
        if (position == left) {
            myName.setText(player.getName());
            myAddress.setText("玩家地址: " + player.getAddress());
            myGrade.setText(String.valueOf(player.getGrade()));
        } else {
            otherName.setText(player.getName());
            otherAddress.setText("玩家地址: " + player.getAddress());
            otherGrade.setText(String.valueOf(player.getGrade()));
        }
    }

    /**
     * 设置信息
     */
    public static void setGrade(int grade, int position) {
        if (position == left) {
            myGrade.setText(String.valueOf(grade));
        } else {
            otherGrade.setText(String.valueOf(grade));
        }
    }
}
